package com.abc.chatprj;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
@WebFilter("/chat/*")
public class AuthenFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        Object myself = req.getSession().getAttribute("myself");
        if(myself != null){
            chain.doFilter(request, response);
        }else{
            //获取到项目的访问路径
            String contextPath = req.getServletContext().getContextPath();
            System.out.println(contextPath);
            HttpServletResponse resp = (HttpServletResponse) response;
            resp.sendRedirect(contextPath+"/index.jsp");
        }

    }
}
